//
//  ChartTableCell.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/29.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "ChartTableCell.h"
#import "ChartTableModel.h"
#import "NSString+StringSize.h"
@interface ChartTableCell()
@property (nonatomic,strong)UILabel *leftLabel;
@property (nonatomic,strong)UILabel *centerLabel;
@property (nonatomic,strong)UILabel *rightLabel;
@end

@implementation ChartTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setLength1:(CGFloat)length1
{
    _length1 = length1;
 
}
- (void)setLength2:(CGFloat)length2
{
    _length2 = length2;
   
}
- (void)setModel:(ChartTableModel *)model
{
    _model = model;
    
}

- (void)setUpUI
{
    _leftLabel= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:@"车牌号"];
    [self addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(31);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    _centerLabel= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:right andString:@"里程数(km)"];
    [self addSubview:_centerLabel];
    [_centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX).offset(-kScreenWidth*0.05);
        make.centerY.mas_equalTo(_leftLabel);
    }];
    
    _rightLabel= [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:@"排名"];
    [self addSubview:_rightLabel];
    [_rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-31);
        make.centerY.mas_equalTo(_leftLabel);
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size1 = [_model.leftString sizeWithLabelWidth:200 font:[UIFont systemFontOfSize:NormalFontSize]];
    if (size1.width>_length1) {
        [_leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(31);
            make.width.mas_equalTo(size1.width);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
    }else{
        [_leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(31);
            make.width.mas_equalTo(_length1);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
    }
    
    CGSize size2 = [_model.rightString sizeWithLabelWidth:400 font:[UIFont systemFontOfSize:NormalFontSize]];
    if (size2.width>_length2) {
        if (_backReloadUI) {
            _backReloadUI([NSString stringWithFormat:@"%lf",size2.width]);
        }
        [_rightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-31);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
        [_centerLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX).offset(-kScreenWidth*0.1);
            make.centerY.mas_equalTo(self);
        }];
    }else{
        [_rightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-31);
            make.width.mas_equalTo(_length2);
            make.centerY.mas_equalTo(self.mas_centerY);
        }];
    }
    _leftLabel.text = _model.leftString;
    _rightLabel.text = _model.rightString;
    _centerLabel.text = _model.centerString;
}

@end
